<?php
namespace Bus\Controller;
use Think\Controller;
class StationarticleController extends Controller {
	    function stationarticle2(){
		
		$did=I('did');
         $l_detail=array(
         'did'=>$did
            );
         $rowt=D('line_up_down')
			 ->where($l_detail)
			 ->find();

			$rowt['station_name']=$_GET['station_name'];
			$appname=C('AK_APPNAME');
			$appkey=C('AK_APPKEY');
			$ak="name$appname$appkey";
			$rowt['ak']=$ak;
		
            $datenow=1440-date('H')*60+date('i')+$rowt['lineid'];
            if($rowt['sj']==0)
            {
                $datenow=$datenow+strlen($rowt['station_name']);
            }
            else
            {
                $datenow=$datenow-strlen($rowt['station_name']);
            }

            $datenow=$datenow/1.9;


			$rowt['stations']=7-$datenow%8;

			$rowt['minutes']=round($rowt['stations']*1.9);

        $rowt2['datenum']=time();
         $rowt2['remainder']=$rowt2['datenum'] % 2; 
         $rowt2['id'] =$_GET['id'];
                   
         $this->assign('buslist1',$rowt);
         $this->assign('buslist2',$rowt2);


         layout('base');
         $this->display();
    }

        function stationarticle(){
        
        $did=I('did');
         $l_detail=array(
         'did'=>$did
            );
         $rowt=D('line_up_down')
             ->where($l_detail)
             ->find();

            $rowt['station_name']=$_GET['station_name'];
            $appname=C('AK_APPNAME');
            $appkey=C('AK_APPKEY');
            $ak="name$appname$appkey";
            $rowt['ak']=$ak;
        

        $host=C('Host');  
        $url = 'http://'.$host.'/third/transit/getAllBusInfoByStationNameAndSj';        //$testSignValue = "100340700b8a4be8b8275859c4ee622e";//测试sign        
        $testSignValue = C('AK_APPKEY');//测试sign  
              
        $pName = "name";
        $pNameValue = C('AK_APPNAME');
        
        $pStationName = "stationName";
        $pStationNameValue =$rowt['station_name'];

        $pSj = "sj";
        $pSjValue = $rowt['sj'];
        
        $plineid = "lineid";
        $plineidValue = $rowt['lineid'];
        
    
        $pSign = "sign";
        //$pSignValue = md5($pName.$pNameValue/** 在此添加其它参数 **/.$testSignValue);
        //$pSignValue = md5($pName.$pNameValue.$pStationName.$pStationNameValue.$pSj.$pSjValue.$plineid.$plineidValue.$testSignValue);
        
        $pSignValue =md5($plineid.$plineidValue.$pName.$pNameValue.$pSj.$pSjValue.$pStationName.$pStationNameValue.$testSignValue);
        
        
        $post_data[$pName]       = $pNameValue;
        $post_data[$pStationName]       = $pStationNameValue;
        $post_data[$pSj]       = $pSjValue;
        $post_data[$plineid]       = $plineidValue;
        $post_data[$pSign]      = $pSignValue;    //'65f246ed358ea52bd60654a2a0aa3206'; 

        $o = "";
        foreach ( $post_data as $k => $v ) 
        { 
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);

        
        if (empty($url) || empty($post_data)) {
            return false;
        }
        
        $postUrl = $url;
        $curlPost = $post_data;
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        



        $res = $data;       

        $result = base64_decode($res);      


            $akData=explode('|',$result);
            $rowt['minutes']=round($akData[4]*1000/$akData[5]);
            $rowt['stations']=$akData[6];




        $rowt2['datenum']=time();
         $rowt2['remainder']=$rowt2['datenum'] % 2; 
         $rowt2['id'] =$_GET['id'];
                   
         $this->assign('buslist1',$rowt);
         $this->assign('buslist2',$rowt2);


         layout('base');
         $this->display();
    }


    public function stationarticle3()
    {
        $lineid=$_GET['lineid'];
        $sj=$_GET['sj'];
        //$pointx=$_POST['pointx'];
        //$pointy=$_POST['pointy'];

        $pointx=cookie('pointx');
        $pointy=cookie('pointy');

        //$pointx=112.37741447126;
        //$pointy=34.647976865564;

        //dump($pointx);
        //dump($pointy);
        //由于  lyzhgj_tr_line_detail表的ID经常更新，所以使用上下行来当参数,下面获取ID的不要了
        //$data=D('lyzhgj_tr_line_detail')->where("line_id='$lineid' and sj='$sj' and del_flag=0")->getField('id');
        //dump($data);
        //$list=$data->select();

        $line=D('line_up_down')->where("lineid=$lineid and sj=$sj")->getField('did');
        //$linedid=$line->select();
        //dump( $line);
        $stationname=M('station_info')->where("line_detail_id='$line'")->order('distance')->limit(1)->field("SQRT(POW( ABS(longitude-'$pointx'), 2) + POW( ABS(latitude-'$pointy'), 2 )) AS distance,station_name");
        //Point((-73.951368+(3/111)), (40.716743+(3/111)))//如果算距离，1纬度或经度为111公里
        $station=$stationname->select();
        //dump($station);  
        //由于  lyzhgj_tr_line_detail表的ID经常更新，所以使用上下行来当参数
        //$list2['stationId']=$data;
        $list2['stationId']=$sj;
        $list2['lineId']=$lineid;
        $list2['stationName']=$station[0]['station_name'];
        //dump($list2);
        $this->assign('list',$list2);
        $this->display();
    }





}